package dao.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import utils.Functions;
import model.Poll;
import model.User;
import dao.DAOUser;

public class DAOMySqlUser extends DAOMySQL implements DAOUser {
    /*get user by id*/
	public User get(int id) {
		Connection connection = connect();
		Statement statement;
		ResultSet resultSet;
		User User;
		
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM a_chatusers WHERE id = " + id);
		} catch (SQLException e) {
			resultSet = null;
		} 
		
		
		try {
			resultSet.next();
			User = new User(resultSet.getString(2), resultSet.getString(3), resultSet.getString(4));
		} catch (SQLException e) {
			User = null;
		} catch(NullPointerException e) {
			User = null;
		} finally {
			disconnect(connection);
		}
		
		return User;
	}
    /*load user by name*/ 
	public ArrayList<User> load(String name) {
		Connection connection = connect();
		Statement statement;
		ResultSet resultSet;
		ArrayList<User> users = new ArrayList<User>();
		
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM a_chatusers WHERE name = '" + name + "' ORDER BY id");
		} catch (SQLException e) {
			resultSet = null;
		}
		
		try {
			while(resultSet.next())
				users.add(new User(resultSet.getString(2), resultSet.getString(3), resultSet.getString(4)));
				
		} catch (SQLException e) {
			users.clear();
		} catch(NullPointerException e) {
			users.clear();
		} finally {
			disconnect(connection);
		}
		
		return users;
	}
    
	/*remove user*/
	public void remove(User user) {
		Connection connection = connect();
		Statement statement;
		
		try {
			statement = connection.createStatement();
			statement.executeUpdate("DELETE FROM a_chatusers WHERE id = " + user.getID());
		} catch (SQLException e) {
			/**/
		} catch (NullPointerException e) {
			/**/
		} finally {
			disconnect(connection);
			user.setID(-1);
		}
		
	}

	/*save user in the database*/
	public void save(User user) throws SQLException {
		Connection connection = connect();
		Statement statement;
		ResultSet resultset;
		
		try {
			if(user.getID() != -1)
				throw new SQLException();
			
			
			statement = connection.createStatement();
			statement.executeUpdate("INSERT INTO a_chatusers VALUES (NULL, '" + user.getName() + "',' " + user.getPassword() + "', '" + user.getEmail() + "', '" + new java.sql.Timestamp(Functions.now().getTime())  + "','" + new java.sql.Timestamp(Functions.now().getTime())  + "')");
			resultset = statement.executeQuery("SELECT LAST_INSERT_ID()");
			resultset.next();
			user.setID(resultset.getInt(1));
			
		} catch (NullPointerException e) {
			throw new SQLException();
		} finally {
			disconnect(connection);
		}
		
	}
    /*save user or update the user if he already exists*/
	public void saveOrUpdate(User user) {
		Connection connection = connect();
		Statement statement;

		try {
			//new user
			this.save(user);
			
		} catch (SQLException e) {

//			if(user == null)
//				throw new SQLException();
//			
//			//Update user
//			statement = connection.createStatement();
//			statement.executeUpdate("UPDATE a_chatusers SET name = '" + user.getName() + "', email = " + user.getEmail() + " WHERE id = " + user.getID());
			
		} finally {
			disconnect(connection);
		}
	}
	
	public void saveBuddy(User user, User buddy) throws SQLException {
		Connection connection = connect();
		Statement statement;
		ResultSet resultset;
		System.out.println("inside save buddy");
		try {
			if(user.getID() != -1)
				throw new SQLException();
			
			
			statement = connection.createStatement();
			statement.executeUpdate("INSERT INTO buddy-list VALUES ( '" + user.getID() + "',' " + buddy.getID() + "', '" + user.getBuddyState(buddy) + "', '" + true);
			resultset = statement.executeQuery("SELECT LAST_INSERT_ID()");
			resultset.next();
			user.setID(resultset.getInt(1));
			
		} catch (NullPointerException e) {
			throw new SQLException();
		} finally {
			disconnect(connection);
		}
		
	}
	
	public void saveOrUpdateBuddy(User user, User buddy) throws SQLException {
		Connection connection = connect();
		Statement statement;

		try {
			//new user
			System.out.println("save buddy");
			this.saveBuddy(user, buddy);
			
		} catch (SQLException e) {

			if(user == null)
				throw new SQLException();
			
			//Update buddy
			System.out.println("Update buddy");
			statement = connection.createStatement();
			statement.executeUpdate("UPDATE buddy-list SET state = '" + user.getBuddyState(buddy) + "WHERE user_id = " + user.getID() + "AND buddy_id = " + buddy.getID());
			
		} finally {
			disconnect(connection);
		}
	}
	@Override
	public ArrayList<User> load() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public User login(String name, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	/*public HashMap<User, UserRelationState> getBuddyList(User user) {
		Connection connection = connect();
		Statement statement;
		ResultSet resultSet;
		HashMap<User, UserRelationState> buddyList = new HashMap<User, UserRelationState>();
		
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM buddy-list WHERE user_id = '" + user.getID() + "'WHERE visible = '" + true + "' ORDER BY id");
		} catch (SQLException e) {
			resultSet = null;
		}
		
		try {
			while(resultSet.next())
				buddyList.put(this.get(resultSet.getInt(1)), UserRelationState.valueOf(resultSet.getString(3)));
				
		} catch (SQLException e) {
			buddyList.clear();
		} catch(NullPointerException e) {
			buddyList.clear();
		} finally {
			disconnect(connection);
		}
		
		return buddyList;
	}

	/**
	 * a a single user to poll
	 *
	public void addUserToPoll(User user, Poll poll) throws SQLException {
		// TODO Auto-generated method stub
		
	}
	/**
	 * add a set of users to a poll 
	 *
	public void addUsersToPoll(ArrayList<User> users, Poll poll)
			throws SQLException {
		// TODO Auto-generated method stub
		
	}
	/**
	 * create a poll 
	 *
	public void createPoll(User user, Poll poll) throws SQLException {
		// TODO Auto-generated method stub
		
	}
    /**
     * get a poll by his id
     *
	public void getPoll(Integer pollId) throws SQLException {
		// TODO Auto-generated method stub
		
	}*/
		
	}
		


